﻿using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using Red.Controls;
using Red.Controls.DataDriven;
using Red.Controls.ViewModels;

namespace Red.Toolkit.WorldEditor
{
    /// <summary>
    /// Interaction logic for SceneTreeItemView.xaml
    /// </summary>
    [InteropControl( "WorldTreeWorld", "WorldTreeArea", "WorldTreeSector" )]
    public partial class SceneTreeItemView : UserControl
    {
        public SceneTreeItemView()
        {
            InitializeComponent();
            DataContextChanged += OnDataContextChanged;
        }

        private void OnDataContextChanged( object sender, DependencyPropertyChangedEventArgs e )
        {
            var treeItem = e.NewValue as InteropTreeItem;

            if ( treeItem == null )
                return;

            InitVisuals( treeItem );
        }

        private void InitVisuals( InteropTreeItem treeItem )
        {
            List<RedMenuDataItem> menu = new List<RedMenuDataItem>();
            switch ( treeItem.Type )
            {
                case "WorldTreeWorld":
                    treeItem.Image = FindResource( "appbar_globe" ) as Canvas;
                    menu = WorldEditorCommands.WorldItemCommands.Select( command => new RedMenuDataItem( new CommandWrapper( command, treeItem ) ) ).ToList();
                    break;

                case "WorldTreeArea":
                    treeItem.Image = FindResource( "appbar_page_corner_grid" ) as Canvas;
                    menu = WorldEditorCommands.AreaItemCommands.Select( command => new RedMenuDataItem( new CommandWrapper( command, treeItem ) ) ).ToList();
                    break;

                case "WorldTreeSector":
                    treeItem.Image = FindResource( "appbar_folder" ) as Canvas;
                    menu = WorldEditorCommands.SectorItemCommands.Select( command => new RedMenuDataItem( new CommandWrapper( command, treeItem ) ) ).ToList();
                    break;

                default:
                    break;
            }
            treeItem.SetMenu( menu );
        }
    }
}
